/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_FLUTTER_NODE_H
#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_FLUTTER_NODE_H

#include "flutter/flow/layers/container_layer.h"
#include "flutter/flow/layers/layer.h"

#include "base/geometry/ng/rect_t.h"
#include "core/components/common/properties/color.h"

namespace OHOS::Ace::NG {

class FlutterNode {
public:
    explicit FlutterNode(bool isRoot) : isRootNode_(isRoot) {}
    ~FlutterNode() = default;

    void SetFrameRect(const RectF& rect)
    {
        if (frameRect_ != rect) {
            needUpdateBackgroud_ = true;
        }
        frameRect_ = rect;
    }
    const RectF& FrameRect()
    {
        return frameRect_;
    }

    void SetBgColor(const Color& color)
    {
        if (bgColor_ != color) {
            needUpdateBackgroud_ = true;
        }
        bgColor_ = color;
    }
    const Color& BgColor()
    {
        return bgColor_;
    }

    void AddPicture(sk_sp<SkPicture> picture);

    void AddChild(const std::shared_ptr<FlutterNode>& node);
    void ClearChildren();

    void Dump(int32_t depth);

    std::shared_ptr<flutter::Layer> BuildLayer(int32_t instanceId);

private:
    void GenBackgroundLayer();
    std::shared_ptr<flutter::PictureLayer> CreatePictureLayer(sk_sp<SkPicture> picture, int32_t instanceId);

    sk_sp<SkPicture> lastPicture_;
    sk_sp<SkPicture> backgroundPicture_;
    std::vector<std::shared_ptr<FlutterNode>> children_;
    RectF frameRect_;
    Color bgColor_ = Color::TRANSPARENT;
    bool needUpdateBackgroud_ = false;
    bool isRootNode_ = false;
};

} // namespace OHOS::Ace::NG

#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_ADAPTER_FLUTTER_NODE_H
